﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.user
{
    public partial class pwd_reset : System.Web.UI.Page
    {
        public Model.member member;
        public int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = InputUtil.Get("uid", 0);
            string reset_key = InputUtil.Get("rid");
            member = new BLL.member().GetModel(id);
            if (member == null)
            {
                Response.Redirect("~/error.aspx?s=用户不存在");
            }
            else
            {
                DateTime expire = new DateTime();
                if (DateTime.TryParse(member.reset_expires, out expire))
                {
                    if (DateTime.Compare(DateTime.Now, expire) < 0)
                    {
                        if (member.reset_key != reset_key)
                        {
                            Response.Redirect("~/error.aspx?s=无效链接");
                        }
                    }
                    else
                    {
                        Response.Redirect("~/error.aspx?s=该链接已过期，请重新发送邮件");
                    }
                }
                else
                {
                    Response.Redirect("~/error.aspx?s=无效链接");
                }
            }
        }

        protected void btnSubmit_Click1(object sender, EventArgs e)
        { 
            //检查验证码
            string checkcode = txtCheckcode.Text;
            if (Session["VerifyCode"] == null)
            {
                lblMsg.Text = "验证码已过期";
                return;
            }
            if (Session["VerifyCode"].ToString().ToLower() != checkcode.ToLower())
            {
                lblMsg.Text = "验证码错误，请重新输入";
                return;
            }

            BLL.member mgr = new BLL.member();
            member.user_pwd = CryptoHelper.DESEncrypt(txtNewPwd.Text.Trim());
            member.reset_key = "";
            member.reset_expires = "";
            mgr.Update(member);
            lblMsg.Text = "密码修改成功！ <a href='/user/login.aspx'>立即登录</a>";
        }
    }
}